<?php use_helper('I18N', 'Date') ?>
<?php include_partial('product/assets') ?>

<script>
	function deleteFile(file_id)
	{
	    var url = $("#hid_delete_file_url").val();
	    var data = {"file_id":file_id};
	    $.post(
	        url,
	        data,
	        function(data) {
	        	$("#ul_"+file_id).remove(); 		
	        });	
	}

	function onDownloadTypeChange()
	{
		var isCreateThubnail = 0;	
		if($("#sel_download_types").find("option:selected").text() == "Product picture")
		{
			isCreateThubnail = 1;
		}
		swfu.setPostParams({"isCreateThumbnail":isCreateThubnail});
	}

	function onProductTypeChange()
	{
		var produc_type_id = $("#product_product_type_id").val();
	    var url = $("#hid_url").val();
	    var data = {"product_type_id":produc_type_id};
	    $("#sel_download_types").attr("disabled",true);
	    $.post(
	        url,
	        data,
	        function(data) {
	        	$("#sel_download_types").attr("disabled",false);
		       $("#sel_download_types").html(data);

		       $("#sel_download_types option").each(function(){
					if($(this).html() == "Product picture")
					{
						$(this).attr("selected","selected")
					}
			       });
		
	        });		
	}	
</script>

<div id="sf_admin_container">
  <h1><?php echo __('Edit Product', array(), 'messages') ?></h1>

  <?php include_partial('product/flashes') ?>

  <div id="sf_admin_header">
  </div>

  <div id="sf_admin_content">
	<div class="sf_admin_form">
	  <form action="<?php echo url_for('product/edit')?>" method="post">    
	  	<input type="hidden" id="product_id"  name="product_id" value="<?php echo $product->getId();?>">
	  	<input type="hidden" id="hid_url" value="<?php echo url_for("product/getDownloadTypeByProductTypeId")?>"/>
	  	<input type="hidden" id="hid_delete_file_url" value="<?php echo url_for("product/deleteFileByFileId")?>"/>
	  	<fieldset id="sf_fieldset_none">
	        <div class="sf_admin_form_row sf_admin_foreignkey sf_admin_form_field_category_id">
	        	<div>
	      			<label>Category</label>
	      			<div class="content">
	      				<?php echo $product->getCategory()->getName();?>
					</div>
	
	          </div>
	  		</div>
	        <div class="sf_admin_form_row sf_admin_foreignkey sf_admin_form_field_product_type_id">
	        	<div>
		      		<label>Product type</label>
		      		<div class="content">
						<?php echo $product->getProductType()->getName();?>
					</div>
	          	</div>
	  		</div>
	    	<div class="sf_admin_form_row sf_admin_text sf_admin_form_field_name">
	        	<div>
	      			<label>Name</label>
	      			<div class="content">
	      				<input type="text" id="product_name" name="product_name" value="<?php echo $product->getName();?>">
	      			</div>
	          	</div>
	  		</div>
	  		
		   <div class="sf_admin_form_row sf_admin_text sf_admin_form_field_name">
		   	<div>
		      <div>
				<?php foreach ($downloadLinks as $downloadLink):?>
				 <ul class="sf_admin_actions" id="ul_<?php echo $downloadLink->getId();?>">  
				<?php if($downloadLink->getDownloadType()->getName() == "Product picture" && strpos($downloadLink->getOriginalName(),"small_") !== false):?> 
					<img src="<?php echo $downloadLink->getFileName(); ?>"/>
				<?php else:?>
				    <li class="sf_admin_action_delete"><a href="javascript:deleteFile(<?php echo $downloadLink->getId();?>)" ></a></li>
					<?php echo $downloadLink->getDownloadType()->getName();?> 
					-
					<?php echo $downloadLink->getOriginalName();?>
				<?php endif;?>
				</ul>					
				<?php endforeach;?>
		      </div>
		      </div>
		    </div>	
	      </fieldset>
		<ul class="sf_admin_actions">  
		  <?php echo $helper->linkToDelete($product, array(  'params' =>   array(  ),  'confirm' => 'Are you sure?',  'class_suffix' => 'delete',  'label' => 'Delete',)) ?>
		  <?php echo $helper->linkToList(array(  'params' =>   array(  ),  'class_suffix' => 'list',  'label' => 'Back to list',)) ?>
		  <li class="sf_admin_action_save"><input type="submit" value="Save"></li>
		</ul>

	  </form>
	</div>
  </div>

  <div id="sf_admin_footer">
  </div>
</div>
